package com.zhumei.keyscale.weight;

import java.util.*;

public class DataUtils
{
    public static String Byte2Hex(final Byte b) {
        return String.format("%02x", b).toUpperCase();
    }
    
    public static String ByteArrToHex(final byte[] array) {
        final StringBuilder sb = new StringBuilder();
        for (int length = array.length, i = 0; i < length; ++i) {
            sb.append(Byte2Hex(array[i]));
            sb.append(" ");
        }
        return sb.toString();
    }
    
    public static String ByteArrToHex(final byte[] array, int i, final int n) {
        final StringBuilder sb = new StringBuilder();
        while (i < n) {
            sb.append(Byte2Hex(array[i]));
            ++i;
        }
        return sb.toString();
    }
    
    public static byte HexToByte(final String s) {
        return (byte)Integer.parseInt(s, 16);
    }
    
    public static byte[] HexToByteArr(final String s) {
        int length = s.length();
        byte[] array2;
        String s2;
        if (isOdd(length) == 1) {
            ++length;
            final byte[] array = new byte[length / 2];
            final StringBuilder sb = new StringBuilder();
            sb.append("0");
            sb.append(s);
            final String string = sb.toString();
            array2 = array;
            s2 = string;
        }
        else {
            final byte[] array3 = new byte[length / 2];
            s2 = s;
            array2 = array3;
        }
        int i = 0;
        int n = 0;
        while (i < length) {
            final int n2 = i + 2;
            array2[n] = HexToByte(s2.substring(i, n2));
            ++n;
            i = n2;
        }
        return array2;
    }
    
    public static int HexToInt(final String s) {
        return Integer.parseInt(s, 16);
    }
    
    public static String IntToHex(final int n) {
        return Integer.toHexString(n);
    }
    
    public static String bytesToHexString(final byte[] array) {
        final StringBuilder sb = new StringBuilder("");
        if (array != null && array.length > 0) {
            for (int length = array.length, i = 0; i < length; ++i) {
                final String hexString = Integer.toHexString(array[i] & 0xFF);
                if (hexString.length() < 2) {
                    sb.append(0);
                }
                sb.append(hexString.toUpperCase());
            }
            return sb.toString();
        }
        return null;
    }
    
    public static String checkXor(final String s) {
        int i = 0;
        int n = 0;
        while (i < s.length()) {
            final int n2 = i + 2;
            n ^= Integer.parseInt(s.substring(i, n2), 16);
            i = n2;
        }
        return integerToHexString(n);
    }
    
    public static List<String> getDivLines(final String s, final int n) {
        final ArrayList<String> list = new ArrayList<String>();
        final int length = s.length();
        final int n2 = (int)Math.floor(s.length() / n);
        int n3;
        for (int i = 0; i < n2; i = n3) {
            n3 = i + 1;
            list.add(s.substring(i * n, n3 * n));
        }
        if (length % n > 0) {
            list.add(s.substring(n2 * n, s.length()));
        }
        return list;
    }
    
    public static String hexBitwiseXOR(final String s, final String s2) {
        final byte[] hexToByteArr = HexToByteArr(s);
        final byte[] hexToByteArr2 = HexToByteArr(s2);
        final byte[] array = new byte[hexToByteArr.length];
        for (int i = 0; i < hexToByteArr.length; ++i) {
            array[i] = (byte)(hexToByteArr[i] ^ hexToByteArr2[i]);
        }
        return bytesToHexString(array);
    }
    
    public static String integerToHexString(final int n) {
        String s2;
        final String s = s2 = Integer.toHexString(n);
        if (s.length() % 2 != 0) {
            final StringBuilder sb = new StringBuilder();
            sb.append("0");
            sb.append(s);
            s2 = sb.toString();
        }
        return s2.toUpperCase();
    }
    
    public static int isOdd(final int n) {
        return n & 0x1;
    }
    
    public static String sum(final String s) {
        final Iterator<String> iterator = getDivLines(s, 2).iterator();
        int n = 0;
        while (iterator.hasNext()) {
            n += HexToInt(iterator.next());
        }
        final String twoByte = twoByte(IntToHex(n));
        final StringBuilder sb = new StringBuilder();
        sb.append(s);
        sb.append(twoByte);
        return sb.toString().toUpperCase();
    }
    
    public static String twoByte(String string) {
        final int length = string.length();
        int i = 0;
        if (length > 4) {
            return string.substring(0, 4);
        }
        while (i < 4 - string.length()) {
            final StringBuilder sb = new StringBuilder();
            sb.append("0");
            sb.append(string);
            string = sb.toString();
            ++i;
        }
        return string;
    }
}
